package com.hbnu.mysql;

import com.hbnu.iostream.InputStreamReaderDemo1;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Properties;
import java.util.ResourceBundle;

public class DatabaseDemo5 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;

        String driver;
        String url;
        String user;
        String password;

//        ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc");
//        driver = resourceBundle.getString("driverClass");
//        url = resourceBundle.getString("url");
//        user = resourceBundle.getString("user");
//        password = resourceBundle.getString("password");


        InputStream resourceAsStream = DatabaseDemo5.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            driver = properties.getProperty("driverClass");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        try {
            Class.forName(driver);

            connection = DriverManager.getConnection(url,user,password);

            statement = connection.createStatement();

            //执行SQL语句
            String sql = "insert into nncc1(c1,c2,c3) values('John','34','UK')";
            int row = statement.executeUpdate(sql);
            System.out.println("影响了数据库"+row+"条数据");


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            try {
                if (statement != null){
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            try {
                if (connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
